﻿using System;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace VeryCodes.Common
{
    /// <summary>
    /// 常用JavaScript弹出框
    /// </summary>
    public class MessageBox
    {
        private MessageBox()
        {
        }

        /// <summary>
        /// 自定义JavaScript
        /// </summary>
        /// <param name="page">当前页面</param>
        /// <param name="script">js脚本</param>
        public static void ResponseScript(Page page, string script)
        {
            page.ClientScript.RegisterStartupScript(page.GetType(), "message", "<script language='javascript' defer>" + script + "</script>");
        }

        /// <summary>
        /// 弹出信息提示框
        /// </summary>
        /// <param name="page">当前页面</param>
        /// <param name="msg">提示信息内容</param>
        public static void Show(Page page, string msg)
        {
            page.ClientScript.RegisterStartupScript(page.GetType(), "message", "<script language='javascript' defer>alert('" + msg.ToString() + "');</script>");
        }

        /// <summary>
        /// 弹出提示框，确认转向
        /// </summary>
        /// <param name="page">当前页面</param>
        /// <param name="msg">提示信息内容</param>
        /// <param name="url">转向Url</param>
        public static void ShowAndRedirect(Page page, string msg, string url)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("<script language='javascript' defer>");
            builder.AppendFormat("alert('{0}');", msg);
            builder.AppendFormat("window.location.href='{0}'", url);
            builder.Append("</script>");
            page.ClientScript.RegisterStartupScript(page.GetType(), "message", builder.ToString());
        }

        /// <summary>
        /// 给控件添加点击确认提示属性
        /// </summary>
        /// <param name="Control">设置该属性的控件</param>
        /// <param name="msg">提示信息内容</param>
        public static void ShowConfirm(WebControl Control, string msg)
        {
            Control.Attributes.Add("onclick", "return confirm('" + msg + "');");
        }

        /// <summary>
        /// 弹出返回（后退）提示框
        /// </summary>
        /// <param name="page">当前页面</param>
        /// <param name="msg">提示信息内容</param>
        public static void ShowBack(Page page, string msg)
        {
            page.ClientScript.RegisterStartupScript(page.GetType(), "message", "<script language='javascript' defer>alert('" + msg.ToString() + "');history.go(-1);</script>");
        }

    }
}

